/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.CouponConfig;
import com.boxin.ims.modules.momarketing.service.CouponConfigService;

/**
 * 会员积分Controller
 * @author Jakemanse
 * @version 2013-09-09
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/couponConfig")
public class CouponConfigController extends BaseController {

	@Autowired
	private CouponConfigService couponConfigService;
	
	@ModelAttribute
	public CouponConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return couponConfigService.get(id);
		}else{
			return new CouponConfig();
		}
	}
	
	@RequiresPermissions("momarketing:couponConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(CouponConfig couponConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			couponConfig.setUser(user);
		}
        Page<CouponConfig> page = couponConfigService.find(new Page<CouponConfig>(request, response), couponConfig); 
        model.addAttribute("page", page);
		return "modules/momarketing/couponConfigList";
	}

	@RequiresPermissions("momarketing:couponConfig:view")
	@RequestMapping(value = "form")
	public String form(CouponConfig couponConfig, Model model) {
		
		User user = UserUtils.getUser();
		if(couponConfig.getUser() == null){
			couponConfig.setUser(user);
		} 
		
		model.addAttribute("couponConfig", couponConfig);
		return "modules/momarketing/couponConfigForm";
	}

	@RequiresPermissions("momarketing:couponConfig:edit")
	@RequestMapping(value = "save")
	public String save(CouponConfig couponConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, couponConfig)){
			return form(couponConfig, model);
		}
		CouponConfig couponConfig2 = couponConfigService.getByUserIdAndKeywords(couponConfig.getUser().getId(), couponConfig.getKeywords());
		if(couponConfig2 != null  &&couponConfig.getId()!=null&& couponConfig2.getId() != couponConfig.getId()){
			 model.addAttribute("errorInfo", "关键词'"+couponConfig2.getKeywords()+"'已存在!");
			 model.addAttribute("message", "关键词'"+couponConfig2.getKeywords()+"'已存在!");
			return form(couponConfig, model);
		}
		couponConfigService.save(couponConfig);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/couponConfig/?repage";
	}
	
	@RequiresPermissions("momarketing:couponConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		couponConfigService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/couponConfig/?repage";
	}

}
